import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/auth'


// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

export const constantRoutes = [
  {
    path: '/',
    component: () => import('@/layout/extractDataSubsystem'),
    redirect: '/imageManagement',
    children: [
      // 数据管理
      {
        path: '/imageManagement',
        name: 'imageManagement',
        component: () => import('@/views/ImageManagement'),
        meta: {
          requireAuth: true,
          isKeepAlive: true
        },
      },
      // 侦察图像、侦察视频-详情页
      {
        path: '/imageVideoDetail',
        name: 'imageVideoDetail',
        component: () => import('@/components/imageVideo/imageVideoDetail'),
        meta: {
          requireAuth: true,
          isKeepAlive: true
        }
      },
      // 
      // {
      //   path: '/multipleUpload',
      //   name: 'multipleUpload',
      //   component: () => import('@/components/imageVideo/multipleUpload'),
      //   meta: {
      //     requireAuth: true,
      //     isKeepAlive: true
      //   }
      // },
      // 资料文档-详情页
      {
        path: '/docDetail',
        name: 'docDetail',
        component: () => import('@/components/information/docDetail'),
        meta: {
          requireAuth: true,
          isKeepAlive: true
        }
      },
      // 图册管理列表
      /* {
        path: '/pictureManagement',
        name: 'pictureManagement',
        component: () => import('@/views/pictureManagement'),
        redirect: '/pictureList',
        meta: {
          requireAuth: true,
          isKeepAlive: false
        },
        children: [{
          path: '/pictureList',
          name: 'pictureList',
          component: () => import('../components/picture/pictureList')
        },
        {
          path: '/picListDetail',
          name: 'picListDetail',
          component: () => import('../components/picListDetails')
        }]
      }, */
      // 数据统计
      {
        path: '/dataStatistics',
        name: 'dataStatistics',
        component: () => import('@/views/dataStatistics'),
        meta: {
          requireAuth: true,
          isKeepAlive: false
        }
      },
      // 数据整编评价
      {
        path: '/dataEvaluate',
        name: 'DataEvaluate',
        component: () => import('@/views/dataEvaluate'),
        meta: {
          requireAuth: true,
          isKeepAlive: true
        }
      },
      // 数据共享
      {
        path: '/dataSharing',
        name: 'DataSharing',
        component: () => import('@/views/DataSharing'),
        meta: {
          requireAuth: true,
          isKeepAlive: false
        }
      },
      // 目标管理
      {
        path: '/targetManagement',
        name: 'targetManagement',
        component: () => import('@/views/targetManagement'),
        meta: {
          requireAuth: true,
          isKeepAlive: false
        }
      },
      // 标签管理
      {
        path: '/tagsDataManagement',
        name: 'tagsDataManagement',
        component: () => import('@/views/tagsDataManagement'),
        meta: {
          requireAuth: true,
          isKeepAlive: false
        }
      },
      // 用户管理
      {
        path: '/userManagement',
        name: 'userManagement',
        component: () => import('@/views/UserManagement'),
        meta: {
          requireAuth: true,
          isKeepAlive: false
        }
      },
      {
        path: '/labelManagement',
        name: 'labelManagement',
        component: () => import('@/views/LabelManagement'),
        meta: {
          requireAuth: true,
          isKeepAlive: false
        }
      },
      {
        path: '/imagesVideoAddPageTable',
        name: 'imagesVideoAddPageTable',
        component: () => import('@/views/imagesVideoAddPageTable'),
        meta: {
          requireAuth: true,
          isKeepAlive: true
        },
        children: [
          {
            path: '/imagesVideoAddPageTable/imagesVideoAddTable',
            name: 'imagesVideoAddTable',
            component: () => import('@/components/imageVideo/imagesVideoAddTable'),
            meta: {
              requireAuth: true,
              isKeepAlive: true
            }
          },
          {
            path: '/imagesVideoAddPageTable/multipleUpload',
            name: 'multipleUpload',
            component: () => import('@/components/imageVideo/multipleUpload'),
            meta: {
              requireAuth: true,
              isKeepAlive: true
            }
          },
        ]
      },
      /* {
        path: '/tagsManagement',
        name: 'tagsManagement',
        component: () => import('@/views/tagsManagement'),
        meta: {
          requireAuth: true,
          isKeepAlive: false
        }
      }, */

      // 说明页
      {
        path: '/question',
        name: 'question',
        component: () => import('@/layout/question'),
        meta: {
          requireAuth: false,
          isKeepAlive: false
        },
      },
      {
        path: '/server',
        name: 'server',
        component: () => import('@/layout/server'),
        meta: {
          requireAuth: false,
          isKeepAlive: false
        },
      }
    ]
  },
  {
    path: '/showDataSubsystem',
    component: () => import('@/layout/showDataSubsystem'),
    children: [
      {
        path: '/',
        component: () => import('@/layout/extractDataSubsystem'),
        redirect: '/imageManagement',
        children: [
          {
            path: '/imageManagement',
            name: 'imageManagement',
            component: () => import('@/views/ImageManagement'),
            meta: {
              requireAuth: true,
              isKeepAlive: true
            }
          },
          // 图册管理列表
          {
            path: '/pictureManagement',
            name: 'pictureManagement',
            component: () => import('@/views/pictureManagement'),
            meta: {
              requireAuth: true,
              isKeepAlive: false
            }
          },
          {
            path: '/targetManagement',
            component: () => import('@/views/targetManagement'),
            meta: {
              requireAuth: true,
              isKeepAlive: false
            }
          },
          // 数据整编评价
          {
            path: '/dataEvaluate',
            component: () => import('@/views/dataEvaluate'),
            meta: {
              requireAuth: true,
              isKeepAlive: false
            }
          },
          {
            path: '/labelManagement',
            component: () => import('@/views/LabelManagement'),
            meta: {
              requireAuth: true,
              isKeepAlive: false
            }
          },
          {
            path: '/dataSharing',
            component: () => import('@/views/DataSharing'),
            meta: {
              requireAuth: true,
              isKeepAlive: false
            }
          },
          {
            path: '/dataStatistics',
            component: () => import('@/views/dataStatistics'),
            meta: {
              requireAuth: true,
              isKeepAlive: false
            }
          },
        ]
      },
      // 图册管理   atlas-managementa
      {
        path: '/atlasManagement',
        name: 'atlasManagement',
        component: () => import('@/views/AtlasManagement'),
        meta: {
          requireAuth: true,
          isKeepAlive: false
        }
      },
      // 产品管理  ProductManagement
      {
        path: '/productManagement',
        name: 'productManagement',
        component: () => import('@/views/ProductManagement'),
      },
      // 资料展示
      {
        path: '/presentationData',
        name: 'presentationData',
        component: () => import('@/views/PresentationData'),
        meta: { requireAuth: true, }
      },
      // 显示html
      {
        path: '/showHtml',
        name: 'showHtml',
        component: () => import('@/views/showHtml'),
        meta: { requireAuth: true, }
      },
      // 说明页
      {
        path: '/questionShow',
        name: 'questionShow',
        component: () => import('@/layout/question'),
        meta: {
          requireAuth: false,
          isKeepAlive: false
        },
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/layout/login'),
    meta: {
      requireAuth: false,
      isKeepAlive: false
    },
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/layout/register'),
    meta: {
      requireAuth: false,
      isKeepAlive: false
    },
  },
  /* {
    path: '/setPassword',
    name: 'setPassword',
    component: () => import('@/layout/setPassword'),
    meta: {
      requireAuth: false,
      isKeepAlive: false
    },
  }, */
]

const createRouter = () => new VueRouter({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ x: 0, y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}


router.beforeEach(async (to, from, next) => {
  let hasToken = getToken()
  if (to.fullPath == "/login") {
    if (hasToken) {
      next({ path: '/' });
      return
    } else {
      next();
      return
    }
  }
  // 判断该路由是否需要登录权限
  if (to.meta.requireAuth) {
    if (hasToken) {
      next();
    } else {
      next({
        path: '/login'
      })
    }
  }
  else {
    next();
  }
});

export default router